package com.freight.core.common.utils;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONException;
import cn.hutool.json.JSONObject;

import java.util.HashMap;
import java.util.List;

public class JsonUtils {
    /**
     * 根据json数据解析返回一个List<HashMap<String, Object>>集合
     *
     * @param json json数据
     * @return
     */
    public static void getJsonList(String json, Long pid,List<HashMap<String, Object>> dataList) {
        try {
            JSONObject rootObject = new JSONObject(json);
            String code = (String) rootObject.get("code");
            String name = (String) rootObject.get("name");
            Long id = SnowFlakeUtil.nextId();
            HashMap<String, Object> pmap = new HashMap<>();
            pmap.put("code", code);
            pmap.put("name", name);
            pmap.put("id", id);
            pmap.put("type", "0");
            dataList.add(pmap);
            JSONArray feedsArray = rootObject.getJSONArray("areaList");
            for (int i = 0; i < feedsArray.size(); i++) {
                JSONObject sonObject = feedsArray.getJSONObject(i);
                String sonCode = (String) sonObject.get("code");
                String sonName = sonObject.getStr("name");
                Long sonId = SnowFlakeUtil.nextId();
                HashMap<String, Object> map = new HashMap<>();
                map.put("code", sonCode);
                map.put("id", sonId);
                map.put("name", sonName);
                map.put("type", "1");
                map.put("pid", pid);
                dataList.add(map);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
